package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserService userService;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //1. 获取用户id
        Long userId = UserHolder.getUser().getId();

        String redisKey= RedisConstants.FOLLOW_KEY_PREFIX +userId;

        //2. 判断操作类型
        if(isFollow){
            //3. 如果关注,增加数据
            Follow follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(userId);

            boolean success = save(follow);

            // 如果成功, 则在redis中增加记录
            if (success) {
                stringRedisTemplate.opsForSet().add(redisKey,followUserId.toString());
            }

        }else{
            //4. 如果取关, 删除数据
            boolean success = remove(new QueryWrapper<Follow>().eq("follow_user_id", followUserId).eq("user_id", userId));

            // 如果成功,则在redis删除记录
            if (success) {
                stringRedisTemplate.opsForSet().remove(redisKey,followUserId.toString());
            }
        }

        return Result.ok();
    }

    @Override
    public Result followCommons(Long targetUserId) {
        //1. 获取当前用户的id
        Long userId = UserHolder.getUser().getId();

        //2. 查询两个用户之间的共同关注
        String currentUserKey = RedisConstants.FOLLOW_KEY_PREFIX + userId;
        String targetUserKey = RedisConstants.FOLLOW_KEY_PREFIX + targetUserId;
        Set<String> userIdSet = stringRedisTemplate.opsForSet().intersect(currentUserKey, targetUserKey);
        if (userIdSet==null||userIdSet.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }

        //3. 解析为userId集合
        List<Long> userIdList = userIdSet.stream().map(Long::parseLong).collect(Collectors.toList());

        //4. 查询用户并返回
        List<UserDTO> userDTOList = userService.listByIds(userIdList).stream().map(item -> BeanUtil.copyProperties(item, UserDTO.class)).collect(Collectors.toList());

        return Result.ok(userDTOList);
    }

    @Override
    public Result isFollow(Long followUserId) {
        // 获取用户id
        Long userId = UserHolder.getUser().getId();

        //1. 查询关联数据
        Integer count = query().eq("follow_user_id", followUserId).eq("user_id", userId).count();

        //2. 返回
        return Result.ok(count>0);
    }
}
